﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Telephony;

namespace C4F.DevKit.Telephony.Controls
{
    [ToolboxItem(true), ToolboxBitmap(typeof(DialPanel))]
    public partial class DialPanel : UserControl
    {
        #region Members

        private TapiManager _tapiManager;

        #endregion

        #region Properties

        public TapiManager TapiManager
        {
            get
            {
                return _tapiManager;
            }

            set
            {
                _tapiManager = value;

                if (value == null)
                {
                    return;
                }

                try
                {
                    List<Line> lines = _tapiManager.GetAvailableLines(true);
                    foreach (Line line in lines)
                        cmbAvailableLines.Items.Add(line.LineName);

                    cmbAvailableLines.SelectedIndex = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        [Browsable(true)]
        public bool DialOptionsButtonsEnabled
        {
            get
            {
                return dialPad1.Enabled;
            }

            set
            {
                dialPad1.Enabled = value;
                txtPhoneNumber.Enabled = value;
                cmbAvailableLines.Enabled = value;
                btnDisconnect.Enabled = !value;
                if (txtPhoneNumber.Text != string.Empty)
                {
                    btnDial.Enabled = value;
                }
                else
                {
                    btnDial.Enabled = !value;
                }
            }
        }

        [Browsable(true)]
        public bool HoldButtonEnabled
        {
            get
            {
                return btnHold.Enabled;
            }

            set
            {
                btnHold.Enabled = value;
            }
        }

        [Browsable(true)]
        public bool ResumeButtonEnabled
        {
            get
            {
                return btnResume.Enabled;
            }

            set
            {
                btnResume.Enabled = value;
            }
        }

        #endregion

        #region Constructors

        public DialPanel()
        {
            InitializeComponent();
            btnDial.Click += new EventHandler(btnDial_Click);
            btnHold.Click += new EventHandler(btnHold_Click);
            btnResume.Click += new EventHandler(btnResume_Click);
            btnDisconnect.Click += new EventHandler(btnDisconnect_Click);
            txtPhoneNumber.TextChanged += new EventHandler(txtPhoneNumber_TextChanged);
            dialPad1.DigitClicked +=new Controls.DialPad.DigitClickedEventHandler(dialPad1_DigitClicked);
            dialPad1.ClearButtonClicked += new EventHandler(dialPad1_ClearButtonClicked);
            dialPad1.BackButtonClicked += new EventHandler(dialPad1_BackButtonClicked);  
        }

        #endregion

        #region Event Handlers

        private void btnDial_Click(object sender, EventArgs e)
        {
            try
            {
                string targetAddress = txtPhoneNumber.Text;
                string lineName = cmbAvailableLines.SelectedItem.ToString();
                _tapiManager.MakeCall(targetAddress, lineName);
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Phone number is not provided.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                string exception = ex.ToString();
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show("Phone number is not valid.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                string exception = ex.ToString();
            }
            catch (TapiManagerException ex)
            {
                MessageBox.Show("Error! Dialing can not be performed.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                string exception = ex.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
   
        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                _tapiManager.DisconnectCall();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnHold_Click(object sender, EventArgs e)
        {
            try
            {
                _tapiManager.Hold();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnResume_Click(object sender, EventArgs e)
        {
            try
            {
                _tapiManager.Resume();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtPhoneNumber_TextChanged(object sender, EventArgs e)
        {
            btnDial.Enabled = txtPhoneNumber.Text != string.Empty;
        }

        private void dialPad1_DigitClicked(char digitChar)
        {
            txtPhoneNumber.Text += digitChar;
        }

        private void dialPad1_ClearButtonClicked(object sender, EventArgs e)
        {
            txtPhoneNumber.Clear();
        }

        private void dialPad1_BackButtonClicked(object sender, EventArgs e)
        {
            string phoneNumber = txtPhoneNumber.Text;

            if (phoneNumber.Length > 0)
            {
                txtPhoneNumber.Text = phoneNumber.Remove(phoneNumber.Length - 1);
            }
        }

        #endregion
    }
}
